package com.ws.shop.action.front;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.ws.shop.bean.Page;
import com.ws.shop.entity.Article;
import com.ws.shop.entity.ArticleCategories;
import com.ws.shop.service.ArticleCategoriesService;
import com.ws.shop.service.ArticleService;
import com.ws.shop.util.RequestUtil;

@Controller
@RequestMapping(value="/article/*")//请求目录
public class ArticleAction  extends BaseFrontAction {

	@Autowired
	private ArticleService articleService;
	@Autowired
	private ArticleCategoriesService articleCategoriesService;
	@RequestMapping(value="index.htm",method=RequestMethod.GET)//请求地址
	public String index(HttpServletRequest request){
		int id = RequestUtil.getIntParameter(request, "id", 11);
		List<ArticleCategories> listarticalcategories = articleCategoriesService.findCategoriesByfid(7);
		Article article = articleService.findArticleById(id);
		request.setAttribute("articlecate", listarticalcategories);
		request.setAttribute("article", article);
		return "article_index";
	}
	
	@RequestMapping(value="list.htm",method=RequestMethod.GET)
	public String list(HttpServletRequest request,Model model){
		int cid = RequestUtil.getIntParameter(request, "cid", 0);
		int single = RequestUtil.getIntParameter(request, "single", -1);
		int pageno = RequestUtil.getIntParameter(request, Page.PAGE_NO, 1);
		if(single < 0){
			ArticleCategories a = articleCategoriesService.findArticleCategoriesById(cid);
			single = a.getSingle();
		}
		List<ArticleCategories> articleCategoriesParentTree = articleCategoriesService.findArticleCategoriesParentTreeById(cid);
		model.addAttribute("articleCategoriesParentTree", articleCategoriesParentTree);
		model.addAttribute("cid", cid);
		model.addAttribute("single", single);
		if(single == 1){
			Page page = articleService.findArticleListPage(pageno, 15, cid);
			model.addAttribute(Page.PAGE, page);
			return "article_list";
		}else{
			Page page = articleCategoriesService.findChildArticleCategoriesListPage(pageno,15,cid);
			model.addAttribute(Page.PAGE, page);
			return "article_categories_list";
		}
	}
	
	@RequestMapping(value="help.htm",method=RequestMethod.GET)
	public String help(){
		
		return "article_help";
	}
}
